home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-15  |  6.3 KB  |  338 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: main.c,v 1.22 1994/11/17 16:40:42 sob Exp sob $";
  3. #endif
  4.  
  5. /*
  6.  *    Network News Transfer Protocol server
  7.  *
  8.  *    Phil Lapsley
  9.  *    University of California, Berkeley
  10.  *    (Internet: phil@berkeley.edu; UUCP: ...!ucbvax!phil)
  11.  *    Stan Barber
  12.  *    Academ Consulting Services
  13.  *    (Internet: sob@academ.com; UUCP: ...!academ!sob)
  14.  *    Wayne Davison
  15.  *    (Internet: davison@borland.com)
  16.  */
  17.  
  18. #ifdef amiga
  19. int __stack =50000;
  20. #endif
  21.  
  22. #include "common.h"
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #ifndef EXCELAN
  26. #include <netdb.h>
  27. #else
  28. struct sockaddr_in current_peer = { AF_INET, IPPORT_NNTP };
  29. #endif
  30. #include <signal.h>
  31. /* XXX Should be #ifdef VARARGS */
  32. #ifndef USG
  33. #ifdef sun
  34. #include <varargs.h>
  35. #endif
  36. #endif
  37. #ifdef hpux
  38. #include <varargs.h>
  39. #endif
  40.  
  41. #ifdef SETPROCTITLE
  42. char    **Argv = NULL;        /* pointer to argument vector */
  43. char    *LastArgv = NULL;    /* end of argv */
  44. #endif /* SETPROCTITLE */
  45.  
  46. int
  47. main(argc,argv,envp)
  48. int argc;
  49. char **argv, **envp;
  50. {
  51.  
  52. #ifdef ALONE    /* If no inetd */
  53.  
  54.     int            sockt, client, length;
  55.     struct sockaddr_in    from;
  56.     extern int         reaper();
  57. #ifdef LOAD
  58.     register int load;
  59. #endif /* LOAD */
  60.  
  61.     disassoc();
  62.  
  63.     /* fd 0-2 should be open and point to / now. */
  64.  
  65.     /* Let's close all the other FD's just to be sure */
  66.     for(sockt = 3; sockt < 40; sockt++)
  67.         (void) close(sockt);
  68.  
  69. #ifdef SYSLOG
  70. #ifdef LOG_DAEMON
  71.     openlog("nntpd", LOG_PID, SYSLOG);        /* fd 3 */
  72. #else
  73.     openlog("nntpd", LOG_PID);            /* fd 3 */
  74. #endif
  75. #endif /* SYSLOG */
  76.  
  77.  
  78. #ifdef FASTFORK
  79.     num_groups = read_groups();    /* Read active file now (fd 4) */
  80.                     /* and then do it every */
  81. #if 0
  82.     set_timer();            /* so often later */
  83. #endif
  84. #endif /* FASTFORK */
  85.  
  86. #ifndef EXCELAN
  87.     sockt = get_socket();        /* should be fd 4 or 5 */
  88. #ifdef USG
  89.     (void) signal(SIGCLD, SIG_IGN);
  90. #else /* !USG */
  91.     (void) signal(SIGCHLD, reaper);
  92. #endif /* USG */
  93.  
  94. #ifdef DEBUG
  95.     (void) signal(SIGUSR1, debugup);
  96.     (void) signal(SIGUSR2, debugdown);
  97. #endif
  98.  
  99.     if (listen(sockt, SOMAXCONN) < 0) {
  100. #ifdef SYSLOG
  101.         syslog(LOG_ERR, "main: listen: %m");
  102. #endif /* SYSLOG */
  103.         exit(1);
  104.     }
  105. #endif /* EXCELAN */
  106.  
  107. #ifdef SETPROCTITLE
  108.     /*
  109.      *  Save start and extent of argv for setproctitle.
  110.      */
  111.  
  112.     Argv = argv;
  113. #ifdef SDD
  114.     LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
  115. #else /*SDD*/
  116.     while (*envp)
  117.         envp++;
  118.     LastArgv = envp[-1] + strlen(envp[-1]);
  119. #endif /*SDD*/
  120. #endif /* SETPROCTITLE */
  121. #if defined(LOAD) && defined(SETPROCTITLE)
  122.     /* If LOAD and SETPROCTITLE, display load before first accept() */
  123.     load = getla();
  124.     setproctitle("%sing connections: loadav %d",
  125.         load > LOAD ? "reject" : "accept", load);
  126. #endif /* LOAD && SETPROCTITLE */
  127.  
  128.     for (;;) {
  129. #ifdef LOAD
  130.         char oline[NNTP_STRLEN];
  131. #endif /* LOAD */
  132. #ifdef EXCELAN
  133.         int status;
  134.         sockt = 3;
  135.         sockt = get_socket();
  136.         if (sockt < 0)
  137.             continue;
  138. #ifdef USG
  139.         (void) signal(SIGCLD, SIG_IGN);
  140. #endif
  141.         bzero((char *)&from,sizeof(from));
  142.         client = accept(sockt, &from);
  143. #else /* !EXCELAN */
  144.         length = sizeof (from);
  145.         client = accept(sockt, &from, &length);
  146. #endif /* EXCELAN */
  147.         if (client < 0) {
  148. #ifdef SYSLOG
  149. #ifdef EXCELAN
  150.             if (errno != EINTR && errno != 60 )
  151. #else /* !EXCELAN */
  152.             if (errno != EINTR)
  153. #endif /* EXCELAN */
  154.                 syslog(LOG_ERR, "accept: %m\n");
  155. #endif /* SYSLOG */
  156. #ifdef EXCELAN
  157.             close(sockt);
  158.             sleep(1);
  159. #endif /* EXCELAN */
  160.             continue;
  161.         }
  162.  
  163. #ifdef LOAD
  164.         if (( load = getla()) > LOAD ) {
  165. #ifdef SETPROCTITLE
  166.             setproctitle("rejecting connections: loadav %d", load);
  167. #endif /* SETPROCTITLE */
  168.             sprintf( oline, "%d loadav at %d, try later\r\n",
  169.                 ERR_GOODBYE, load );
  170.             write( client, oline, strlen( oline ));
  171. #ifdef SYSLOG
  172.             syslog( LOG_INFO, "loadav at %d, sleeping", load );
  173. #endif /* SYSLOG */
  174.             close( client );
  175.             sleep( 5 );
  176.             continue;
  177.         } else {
  178. #ifdef SETPROCTITLE
  179.             setproctitle("accepting connections: loadav %d", load);
  180. #endif /* SETPROCTITLE */
  181.         }
  182. #endif /* LOAD */
  183.  
  184.         switch (fork()) {
  185.         case    -1:
  186. #ifdef SYSLOG
  187.                 syslog(LOG_ERR, "fork: %m\n");
  188. #endif /* SYSLOG */
  189. #ifdef EXCELAN
  190.                 (void) close(sockt);
  191. #endif /* EXCELAN */
  192.                 (void) close(client);
  193.                 break;
  194.  
  195.         case    0:
  196. #ifdef EXCELAN
  197.                 if (fork())
  198.                     exit(0);
  199.                 bcopy(&from,¤t_peer,sizeof(from));
  200.                 make_stdio(sockt);
  201. #else /* !EXCELAN */
  202.                 (void) close(sockt);
  203.                 make_stdio(client);
  204. #endif /* EXCELAN */
  205. #ifdef USG
  206.                 (void) signal(SIGCLD,SIG_DFL);
  207. #endif /* USG */
  208.                 serve();
  209.                 break;
  210.  
  211.         default:
  212. #ifdef EXCELAN
  213.                 (void) close(sockt);
  214. #else /* !EXCELAN */
  215.                 (void) close(client);
  216. #endif /* EXCELAN */
  217.                 break;
  218.         }
  219.     }
  220.  
  221. #else /* !ALONE */        /* We have inetd */
  222.  
  223. #ifdef LOAD
  224.     {
  225.         register int load;
  226.  
  227. #ifdef SYSLOG
  228. #ifdef LOG_DAEMON
  229.         openlog("nntpd", LOG_PID, SYSLOG);
  230. #else
  231.         openlog("nntpd", LOG_PID);
  232. #endif
  233. #endif /* SYSLOG */
  234.         if (( load = getla()) > LOAD ) {
  235.             printf("%d loadav at %d, try later\r\n", 
  236.                    ERR_GOODBYE, load );
  237.             (void) fflush(stdout);
  238. #ifdef SYSLOG
  239.             syslog( LOG_INFO, "loadav at %d, exiting", load );
  240.             closelog();
  241. #endif
  242.             exit(1);
  243.         }
  244. #ifdef SYSLOG
  245.         closelog();
  246. #endif
  247.     }
  248. #endif /* LOAD */
  249. #ifdef SETPROCTITLE
  250.     /*
  251.      *  Save start and extent of argv for setproctitle.
  252.      */
  253.  
  254.     Argv = argv;
  255. #ifdef SDD
  256.     LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
  257. #else /*SDD*/
  258.     while (*envp)
  259.         envp++;
  260.     LastArgv = envp[-1] + strlen(envp[-1]);
  261. #endif /*SDD*/
  262. #endif /* SETPROCTITLE */
  263.  
  264. #ifdef USG
  265.     (void) signal(SIGCLD,SIG_DFL);
  266. #endif /* USG */
  267.  
  268.     serve();
  269.  
  270. #endif /* ALONE */
  271. }
  272.  
  273. /*
  274.  * clobber argv so ps will show what we're doing.
  275.  * stolen from sendmail
  276.  */
  277. #ifdef SETPROCTITLE
  278. #if defined(sun) || defined(hpux)
  279. /*VARARGS*/
  280. void
  281. setproctitle(va_alist)
  282.     va_dcl
  283. {
  284.     register char *p, *fmt;
  285.     register int i;
  286.     char buf[BUFSIZ];
  287.     va_list ap;
  288.  
  289.     va_start(ap);
  290.     fmt = va_arg(ap, char *);
  291.     
  292.     (void) vsprintf(buf, fmt, ap);
  293.  
  294.     /* make ps print "(nntpd)" */
  295.     p = Argv[0];
  296.     *p++ = '-';
  297.  
  298.     i = strlen(buf);
  299.     if (i > LastArgv - p - 2) {
  300.     i = LastArgv - p - 2;
  301.     buf[i] = '\0';
  302.     }
  303.     strcpy(p, buf);
  304.     p += i;
  305.     while (p < LastArgv)
  306.     *p++ = ' ';
  307.  
  308.     va_end(ap);
  309. }
  310. #else
  311. /*VARARGS1*/
  312. void
  313. setproctitle(fmt, a, b, c)
  314. char *fmt;
  315. {
  316.     register char *p;
  317.     register int i;
  318.     char buf[BUFSIZ];
  319.  
  320.     (void) sprintf(buf, fmt, a, b, c);
  321.  
  322.     /* make ps print "(nntpd)" */
  323.     p = Argv[0];
  324.     *p++ = '-';
  325.  
  326.     i = strlen(buf);
  327.     if (i > LastArgv - p - 2) {
  328.         i = LastArgv - p - 2;
  329.         buf[i] = '\0';
  330.     }
  331.     (void) strcpy(p, buf);
  332.     p += i;
  333.     while (p < LastArgv)
  334.         *p++ = ' ';
  335. }
  336. #endif /* hpux */
  337. #endif /* SETPROCTITLE */
  338.